<?php
/**
 * Project:     drylamp - rapid application development framework for php
 * File:        Scaffolder.php
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *
 * @copyright 2008 Ilya St.
 * @author Ilya St. <provided at gmail dot com>
 * @version 1.0
 */

abstract class Scaffolder implements Scaffoldable  {
  const INDEX_TOKEN       = "|_INDEX_TOKEN_|";
  const SINGLE_EMIT_TOKEN = "|_SINGLE_EMIT_TOKEN_|";
  const CONTAINER_CONTENT = "|_CONTAINER_CONTENT_|";
  
  abstract protected function getMyTplDir();
  
  protected $mDataFieldsArray = null;
  protected $mIsNullData      = false;
  
  /**
   * Wraps Exposable objects into specified Scaffolder
   *
   * @param mixed $pData
   * @param string $pClassName
   * @return mixed
   */
  public static function wrap($pData, $pClassName) {
    if (is_array($pData)) {
      $retArr = array();
      foreach($pData as $element) {
        if ( ($element instanceof Exposable) == false ) {
          throw new ScaffolderWrapException("Can't wrap not Exposable object. Class of the element is: ". get_class($element));
        }
        $retArr[] = new $pClassName($element);
      }
      return $retArr; 
    }
    elseif ($pData instanceof Exposable) {
      return new $pClassName($pData);
    }
     // taking care of empty results
     elseif (is_null($pData)) {
       $obj = new $pClassName(null);
       return $obj;
     }
   throw new ScaffolderWrapException("Can't wrap not Exposable object"); 
  }
  
  
  /**
   * Called by ScaffoldNode::emit() when there is no data in the data object.
   * Should show an appropriate message, empty string is default
   */
  public function getNoDataTpl() {
    return "";
  }
  
  
  public function isNoData() {
    return is_null($this->mDataFieldsArray) | (count($this->mDataFieldsArray) == 0);
  }
  
  public function __get($name) {
	  return $this->mDataFieldsArray[$name];
  }
  
}


class ScaffolderException extends Exception {};
class ScaffolderWrapException extends ScaffolderException{};